/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controllers.sysAdmin;

import controllers.utils.DatabaseHjelper;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
import models.Institute;

/**
 *
 * @author Ida
 */
@SessionScoped
@Named(value="newInstitute")
public class addNewInstitute implements Serializable {
    private Institute newInstitute;
    private DatabaseHjelper dbh = DatabaseHjelper.hent();

    public addNewInstitute() {
        this.newInstitute = new Institute();
    }

    public Institute getNewInstitute() {
        return newInstitute;
    }

    public String addNewInstitute(){
        newInstitute.setName(null);
        newInstitute.setAddress(null);
        newInstitute.setPhoneNr(null);
        newInstitute.setNote(null);
        return "addNewInstitute";
    }

     public String executeNewInstitute(){
        addNewInstituteToDB();
        return "addNewInstituteOutput";
    }

      private void addNewInstituteToDB() {
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            conn = dbh.finnForbindelse();
            stmt = conn.prepareStatement("INSERT INTO institute(instituteName, instAddress, instPhoneNr, instNote) VALUES(?, ?, ?, ?)");
            stmt.setString(1, newInstitute.getName());
            stmt.setString(2, newInstitute.getAddress());
            stmt.setString(3, newInstitute.getPhoneNr());
            stmt.setString(4, newInstitute.getNote());
            stmt.executeUpdate();
        } catch (SQLException e) {
            System.out.println("Feil i lagring av bruker: " + e);
        } finally {
            DatabaseHjelper.lukk(stmt);
            DatabaseHjelper.lukk(conn);
        }
    }

}
